package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.mapper.Pms_Attr_Group_Mapper;
import com.shop.mapper.Pms_Attr_Mapper;
import com.shop.mapper.Pms_Category_Attr_Mapper;
import com.shop.mapper.Pms_Category_Mapper;
import com.shop.pojo.pms.Pms_Attr;
import com.shop.pojo.pms.Pms_Category;
import com.shop.pojo.pms.Pms_Category_Attr;
import com.shop.service.AttrGroupCatalogVoService;
import com.shop.vo.AttrGroupCatalogVo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

@Service
public class AttrGroupCatalogVoServiceImpl implements AttrGroupCatalogVoService {
    @Autowired
    private Pms_Attr_Mapper pms_attr_mapper;
    @Autowired
    private Pms_Attr_Group_Mapper pms_attr_group_mapper;
    @Autowired
    private Pms_Category_Mapper pms_category;
    @Autowired
    private Pms_Category_Attr_Mapper pms_category_attr_mapper;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    /*
     * @Description 通过异步线程的方式来进行查询，
     * 全部异步执行然后将查询的数据封装到我们封装的类里
     * 都执行了属性、分组、分类的全部查询并封装到类里
     * @param
     * @return com.shop.vo.AttrGroupCatalogVo
     * @author liusupeng
     * @date 2020/10/29 12:46
     */
    @SneakyThrows
    @Override
    public AttrGroupCatalogVo ATTR_GROUP_CATALOG_VO(Integer cat_id) {


        AttrGroupCatalogVo attrGroupCatalogVo=new AttrGroupCatalogVo();


        CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() -> {
       System.out.println("我来执行1");
            QueryWrapper<Pms_Category_Attr> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("pca.cat_id",cat_id);
            List<Pms_Category_Attr> attrByCategoryList = pms_category_attr_mapper.findAttrByCategoryList(queryWrapper);
            List<Pms_Attr> collect = attrByCategoryList.stream().map((x) -> {
                Pms_Attr p = new Pms_Attr();
                p.setAttr_name(x.getAttr_name());
                p.setAttr_id(x.getAttr_id());
                return p;
            }).collect(Collectors.toList());
        System.out.println("我是list:"+collect);
            attrGroupCatalogVo.setPms_attrs(collect);


        }, threadPoolExecutor);
        CompletableFuture<Void> voidCompletableFuture2 = CompletableFuture.runAsync(() -> {
            System.out.println("我来执行2");
            QueryWrapper queryWrapper1=new QueryWrapper();
            attrGroupCatalogVo.setPms_attr_groups(pms_attr_group_mapper.selectList(queryWrapper1));
        }, threadPoolExecutor);
        CompletableFuture.allOf(voidCompletableFuture1,voidCompletableFuture2).get();
        return attrGroupCatalogVo;
    }
}
